<template>
    <div class="goods-list">
        
        <!-- <router-link  class="goods-item" v-for="(item,i) in goodsList" 
        :key="i" :to="'/home/goodsinfo/' + item.goods_id" tag="div">
            <img :src= "item.goods_image_url" alt="">
            <h1 class="title">{{ item.goods_name }}</h1>
            <div class="info">
                <p class="price">
                <span class="now">￥{{ item.min_group_price }}</span>
                <span class="old">￥{{ item.min_normal_price }}</span>
                </p>
                <p class="sale">
                    <span>热卖中</span>
                    <span>{{ item.coupon_remain_quantity }}</span>
                </p>
            </div>
        </router-link> -->

        <!--网页中的两种跳转方式： 
        1. 使用a标签(标签跳转)  
        2. 使用window.location.href的形式(编程式跳转)
        -->
        <div  class="goods-item" v-for="(item,i) in goodsList" 
        :key="i" @click="goDetail(item.goods_id)">
            <img :src= "item.goods_image_url" alt="">
            <h1 class="title">{{ item.goods_name }}</h1>
            <div class="info">
                <p class="price">
                <span class="now">￥{{ item.min_group_price }}</span>
                <span class="old">￥{{ item.min_normal_price }}</span>
                </p>
                <p class="sale">
                    <span>热卖中</span>
                    <span>{{ item.coupon_remain_quantity }}</span>
                </p>
            </div>
        </div>

       <mt-button type="danger" size="large" @click="getMore">加载更多</mt-button>   
    </div>
</template>

<script>

const API_PROXY = 'https://bird.ioliu.cn/v1/?url=' 
import { Toast } from 'mint-ui'

export default {
    data(){
        return {
            key: '89c62486-da1e-04d3-d2a2-fef18a9829b6',
            goodsList: []
        }
    },
    created() {
        this.getGoodsList()
    },
    methods:{
        getGoodsList(){
            let _this = this
            _this.$axios.get(API_PROXY +'http://api.web.21ds.cn/pinduoduo/getTopGoodsList?apkey=' + _this.key)
                        .then(function (response) {
                         console.log(response.data.data.list)
                         _this.goodsList = _this.goodsList.concat(response.data.data.list)
                        })
                        .catch(function (err) {
                        Toast(err)
                        })
            },
            getMore(){
                  //使用Js的形式进行路由导航
            },
            goDetail(id){

                 //注意: this.$route和this.$router的区别
                 //this.$route是路由参数对象, 如params, query..
                 //this.$router是路由导航对象,用它可以方便地使用js代码实现路由的跳转
                //1. 直接将路由写成字符串传递
                //this.$router.push('/home/goodsinfo/' + id)
                //2. 传递路由对象
                //this.$router.push({path: '/home/goodsinfo/' + id})
                //3. 传递一个已命名的路由
                this.$router.push({name: 'goodsinfo', params: { id: id }})
            }
    
        }
}
</script>

<style lang="scss" scoped>
.goods-list{
    
    display: flex;
    flex-wrap: wrap;
    padding: 6px;
    justify-content: space-between;

    .goods-item{
        width: 49%;
        border: 1px solid #ccc;
        box-shadow: 0 0 8px #ccc;
        margin: 3px 0;
        padding: 2px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        min-height: 289px;
        img{
            width: 100%;
        }
        .title{
            font-size: 14px;
        }

        .info{
            background-color: #eee;
            p{
                margin: 0;
                padding: 4px;
            }
            .price{
                .now{
                    color: red;
                    font-weight: bold;
                }
                .old{
                    text-decoration: line-through;
                    font-size: 12px;
                    margin-left: 10px;
                }
            }
            .sale{
               
                display: flex;
                justify-content: space-between;
                font-size: 13px;
            }
        }
    }

}
</style>